package com.atguigu.gmall.product.service.impl;


import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.model.product.SpuSaleAttrValue;
import com.atguigu.gmall.product.mapper.SpuImageMapper;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.mapper.SpuSaleAttrValueMapper;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author h8467
 * @description 针对表【spu_info(商品表)】的数据库操作Service实现
 * @createDate 2022-11-01 14:01:54
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
        implements SpuInfoService {
    @Autowired
    SpuImageMapper spuImageMapper;

    @Autowired
    SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    @Override
    public void saveSpuInfo (SpuInfo spuInfo) {
        // 1、新建品牌
        this.save (spuInfo);
        Long id = spuInfo.getId ();
        // 2、上传照片
        for (SpuImage image : spuInfo.getSpuImageList ()) {
            image.setSpuId (id);
            this.spuImageMapper.insert (image);
        }

        // 3、上传Spu销售信息
        if (spuInfo.getSpuSaleAttrList ().size () > 0) {
            for (SpuSaleAttr spuSaleAttr : spuInfo.getSpuSaleAttrList ()) {
                spuSaleAttr.setSpuId (id);
                this.spuSaleAttrMapper.insert (spuSaleAttr);
                if (spuSaleAttr.getSpuSaleAttrValueList ().size () > 0) {
                    for (SpuSaleAttrValue spuSaleAttrValue : spuSaleAttr.getSpuSaleAttrValueList ()) {
                        spuSaleAttrValue.setSpuId (id);
                        spuSaleAttrValue.setSaleAttrName (spuSaleAttr.getSaleAttrName ());
                        this.spuSaleAttrValueMapper.insert (spuSaleAttrValue);
                    }
                }
            }
        }


    }
}




